#!/usr/bin/python
# -*- coding: utf-8 -*-

from flask import request


class RESTError(StandardError):
    code = 0
    error = 'base_error'
    detail = ''
    status_code = 400

    def __init__(self, detail='', request_=None):
        StandardError.__init__(self)
        if detail:
            self.detail = detail
        if request_ is not None:
            self.request = request_
        else:
            self.request = ' '.join([request.method, request.url])

    def __str__(self):
        return str(self.to_dict())

    def to_dict(self):
        ret = {
            'code': self.code,
            'error': self.error,
            'detail': self.detail,
            'request': self.request,
        }
        return ret


class HTTP400(RESTError):
    status_code = 400


class HTTP401(RESTError):
    status_code = 401


class HTTP404(RESTError):
    status_code = 404


class UnknownError(HTTP400):
    code = 999
    error = 'unknow_error'
    detail = u''


class URINotFound(HTTP404):
    code = 1000
    error = 'uri_not_found'
    detail = u''


class WrongPassword(HTTP400):
    code = 1001
    error = 'wrong_password'
    detail = u''


class UserExistError(HTTP400):
    code = 1002
    error = 'user_exist'
    detail = u''
